package com.qifan.energy.adapter;

import android.content.Context;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.qifan.energy.mvp.callback.IOnClickListener;
import com.qifan.energy.R;
import com.qifan.energy.bean.result.GoodsDetailResult;
import com.qifan.energy.util.CheckUtil;
import com.qifan.energy.util.UIUtils;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ProductDetailSpecAdapter extends BaseQuickAdapter<GoodsDetailResult.ProductAttributeListBean,BaseViewHolder> {

    private Context context;
    private GoodsDetailResult detailResult = null;
    private Map<Integer,Integer> specMap = new HashMap<>();
    private IOnClickListener listener = new IOnClickListener() {
        @Override
        public void onClick() {

        }
    };

    /* 对外提供 */
    public void setIOnClickListener(IOnClickListener listener) {
        this.listener = listener;
    }

    /* 对外提供 */
    public Map<Integer, Integer> getSpecMap() {
        return specMap;
    }

    /* 设置详情规格 */
    public void setDetailResult(@NonNull GoodsDetailResult result){
        this.detailResult = result;
    }

    public ProductDetailSpecAdapter(Context context, @Nullable List<GoodsDetailResult.ProductAttributeListBean> data) {
        super(R.layout.item_section_title_spec, data);
        this.context = context;
    }

    @Override
    protected void convert(@NonNull BaseViewHolder helper, GoodsDetailResult.ProductAttributeListBean item) {
        helper.setText(R.id.title_tv,item.getName());
        LinearLayout contentLl = (LinearLayout)helper.getView(R.id.detail_spec_content_ll);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.setMarginEnd(UIUtils.dp2px(15));
        if (null != detailResult) {
            for (GoodsDetailResult.ProductAttributeValueListBean bean1 : detailResult.getProductAttributeValueList()) {
                if (item.getId() == bean1.getProductAttributeId()) {
                    if (CheckUtil.hasContent(bean1.getValue())) {
                        String[] split = bean1.getValue().split(",");
                        List<String> specList = Arrays.asList(split);
                        for (int i=0; i<specList.size(); i++) {
                            TextView xmlView = (TextView) UIUtils.getXmlView(R.layout.item_product_detail_spec);
                            xmlView.setLayoutParams(layoutParams);
                            xmlView.setText(specList.get(i));
                            xmlView.setTag(i);
                            int finalI = i;
                            xmlView.setOnClickListener(v -> {
                                for (int j = 0; j <contentLl.getChildCount(); j++) {
                                    TextView childAt = (TextView) contentLl.getChildAt(j);
                                    childAt.setSelected(false);
                                }
                                v.setSelected(true);
                                specMap.put(helper.getAdapterPosition(), finalI);
                                if (listener != null) listener.onClick();//点击回调
                            });
                            contentLl.addView(xmlView);
                            if (i == 0) {// 默认选中第一个
                                xmlView.setSelected(true);
                                specMap.put(helper.getAdapterPosition(), finalI);
                            }
                        }
                        break;
                    }
                }
            }
        }
    }
}
